package org.geektimes.rest.client;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.ws.rs.ext.RuntimeDelegate;
import java.io.IOException;

public class DefaultHeaderDelegate<T> implements RuntimeDelegate.HeaderDelegate<T> {
    private Class<T> type;
    private ObjectMapper objectMapper =new ObjectMapper();
    public DefaultHeaderDelegate(Class<T> type) {
        this.type = type;
    }

    @Override
    public T fromString(String value) {
        try {
            return objectMapper.readValue(value,type);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String toString(T value) {
        try {
            return objectMapper.writeValueAsString(value);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }
}
